/********************************** (C) COPYRIGHT *******************************
 * File Name          : KEY.h
 * Author             : WCH
 * Version            : V1.0
 * Date               : 2016/04/12
 * Description        :
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for 
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/

/******************************************************************************/
#ifndef __IO_H
#define __IO_H

#ifdef __cplusplus
extern "C" {
#endif


#define KEY_CODE_NONE               (0x00)
#define KEY_CODE_CLICK              (0x01)
#define KEY_CODE_D_CLICK            (0x02)
#define KEY_CODE_L_CLICK            (0x03)
#define KEY_CODE_T_CLICK            (0x04)

#define ACTIVE_LEVEL_LOW        0
#define ACTIVE_LEVEL_HIGH       1

#define CLICK_KEY_NUM           1

typedef struct
{
    uint8_t act_level;
    uint8_t cur_status;
    uint8_t cnts;
    uint8_t code;
    uint8_t delay;
    uint8_t (*key_read)(void);
}key_obj;

#define KEY1_IO_INIT()          GPIOB_ModeCfg(GPIO_Pin_22, GPIO_ModeIN_PU)
#define KEY1_IO_READ()          ((GPIOB_ReadPortPin(GPIO_Pin_22) == 0x00)?1:0)

extern key_obj input_key[];
extern key_obj debug_key;

void Key_Init(void);
void button_obj_handle(key_obj *obj, uint8_t key_cnt);
void button_obj_clear(key_obj *obj);
uint8_t button_obj_get(key_obj *obj);

#define ENC_CODE_IDLE       0x00
#define ENC_CODE_LEFT       0x01
#define ENC_CODE_RIGHT      0x02

#define ENCODE_NUM          2

#define ENCODE_OBJ1         0x00
#define ENCODE_OBJ2         0x01

extern __IO uint8_t enc_code[ENCODE_NUM];

#define ENC1_GET()          enc_code[ENCODE_OBJ1]
#define ENC1_CLEAR()        enc_code[ENCODE_OBJ1] = ENC_CODE_IDLE
#define ENC2_GET()          enc_code[ENCODE_OBJ2]
#define ENC2_CLEAR()        enc_code[ENCODE_OBJ2] = ENC_CODE_IDLE

#define ENC1_A_IO_INIT()  GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU)
#define ENC1_B_IO_INIT()  GPIOB_ModeCfg(GPIO_Pin_7, GPIO_ModeIN_PU)

#define ENC1_A_READ()   ((GPIOB_ReadPortPin(GPIO_Pin_4) == 0x00)?1:0)
#define ENC1_B_READ()   ((GPIOB_ReadPortPin(GPIO_Pin_7) == 0x00)?1:0)

#define ENC1_READ()     (ENC1_A_READ()|(ENC1_B_READ()<<1))

void Encode_Init(void);

void IO_Init(void);

#ifdef __cplusplus
}
#endif

#endif
